package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbItemExample;
import com.pyg.pojo.TbItemExample.Criteria;
import com.pyg.mapper.TbItemMapper;
import com.pyg.pojo.TbItem;
import com.pyg.goods.service.TbItemService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbItemServiceImpl implements TbItemService {
	@Autowired
	private TbItemMapper tbItemMapper;

	@Override
	public List<TbItem> findAll() {
		return tbItemMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbItem> page = (Page<TbItem>) tbItemMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbItem
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbItem tbItem, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbItemExample example = null;
		if (tbItem != null) {
			example = new TbItemExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbItem.getTitle())) {
				criteria.andTitleLike("%" + tbItem.getTitle() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getSellPoint())) {
				criteria.andSellPointLike("%" + tbItem.getSellPoint() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getPrice())) {
				criteria.andPriceEqualTo(tbItem.getPrice() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getStockCount())) {
				criteria.andStockCountEqualTo(tbItem.getStockCount() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getNum())) {
				criteria.andNumEqualTo(tbItem.getNum() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getBarcode())) {
				criteria.andBarcodeLike("%" + tbItem.getBarcode() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getImage())) {
				criteria.andImageLike("%" + tbItem.getImage() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getCategoryid())) {
				criteria.andCategoryidEqualTo(tbItem.getCategoryid() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getStatus())) {
				criteria.andStatusLike("%" + tbItem.getStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getCreateTime())) {
				criteria.andCreateTimeEqualTo(tbItem.getCreateTime() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getUpdateTime())) {
				criteria.andUpdateTimeEqualTo(tbItem.getUpdateTime() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getItemSn())) {
				criteria.andItemSnLike("%" + tbItem.getItemSn() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getCostPirce())) {
				criteria.andCostPirceEqualTo(tbItem.getCostPirce() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getMarketPrice())) {
				criteria.andMarketPriceEqualTo(tbItem.getMarketPrice() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getIsDefault())) {
				criteria.andIsDefaultLike("%" + tbItem.getIsDefault() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getGoodsId())) {
				criteria.andGoodsIdEqualTo(tbItem.getGoodsId() );
			}
			if (PygStringUtils.isNotEmpty(tbItem.getSellerId())) {
				criteria.andSellerIdLike("%" + tbItem.getSellerId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getCartThumbnail())) {
				criteria.andCartThumbnailLike("%" + tbItem.getCartThumbnail() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getCategory())) {
				criteria.andCategoryLike("%" + tbItem.getCategory() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getBrand())) {
				criteria.andBrandLike("%" + tbItem.getBrand() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getSpec())) {
				criteria.andSpecLike("%" + tbItem.getSpec() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbItem.getSeller())) {
				criteria.andSellerLike("%" + tbItem.getSeller() + "%");
			}
		}

		Page<TbItem> page = (Page<TbItem>) tbItemMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbItem tbItem) {
		tbItemMapper.insert(tbItem);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbItemMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbItem findOne(Long id) {
		return tbItemMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbItem tbItem) {
		tbItemMapper.updateByPrimaryKey(tbItem);
	}

}
